package com.nintendo.controller;

import com.nintendo.base.BaseInfoProperties;
import com.nintendo.pojo.ChatMessage;
import com.nintendo.pojo.netty.NettyServerNode;
import com.nintendo.result.GraceJSONResult;
import com.nintendo.service.ChatMessageService;
import com.nintendo.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/chatmessage")
@Slf4j
public class ChatMessageController extends BaseInfoProperties {
    @Resource
    private ChatMessageService chatMessageService;
    @Resource(name = "curatorClient")
    private CuratorFramework zkClient;
    @PostMapping("/getChatMessage")
    public GraceJSONResult getChatMessage(@RequestParam("myId") String myId,
                                          @RequestParam("friendId") String friendId,
                                          @RequestParam("pageNum") Integer pageNum,
                                          @RequestParam("pageSize") Integer pageSize) {
        log.info("myId:{},friendId:{},pageNum:{},pageSize:{}", myId, friendId, pageNum, pageSize);
        List<ChatMessage> chatMessageListByPage = chatMessageService.findChatMessageListByPage(pageNum, pageSize, myId, friendId);
        return GraceJSONResult.ok(chatMessageListByPage);
    }

    @PostMapping("/findChatMessageAll")
    public GraceJSONResult getChatMessage(@RequestParam("myId") String myId,
                                          @RequestParam("friendId") String friendId) {
        List<ChatMessage> chatMessageListByPage = chatMessageService.findChatMessageListAll(myId, friendId);
        return GraceJSONResult.ok(chatMessageListByPage);
    }


    @PostMapping("/clearMyUnReadCounts")
    public GraceJSONResult clearMyUnReadCounts(@RequestParam("myId") String myId,
                                               @RequestParam("friendId") String friendId) {
        chatMessageService.clearMyUnReadCounts(myId, friendId);
        log.info("myId:{},friendId:{}", myId, friendId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/incrUserMessage")
    public GraceJSONResult incrUserMessage(@RequestParam("myId") String myId,
                                           @RequestParam("friendId") String friendId) {
        Integer count = chatMessageService.incrUserMessage(myId, friendId);
        log.info("myId:{},friendId:{},count:{}", myId, friendId, count);
        return GraceJSONResult.ok(count);
    }
    @PostMapping("/getNettyOnlineInfo")
    public GraceJSONResult getNettyOnlineInfo() throws Exception {
        // 从zookeeper中获取当前已经注册的节点信息
        String path = "/server-list";
        List<String> list = zkClient.getChildren().forPath(path);
        System.out.println(list);
        List<NettyServerNode> serverNodeList = new ArrayList<>();
        for(String node: list){
            String nodeValue =  new String(zkClient.getData().forPath(path + "/" + node));
            System.out.println(nodeValue);
            NettyServerNode serverNode = JsonUtils.jsonToPojo(nodeValue, NettyServerNode.class);
            serverNodeList.add(serverNode);
        }
        // 计算当前那个zk的node是最少连接数,获得[ip:port]并且返回给前端
        Optional<NettyServerNode> minNodeOptional = serverNodeList
                .stream()
                .min(Comparator.comparing(NettyServerNode::getOnlineCounts));
        NettyServerNode minNode = minNodeOptional.get();
        return GraceJSONResult.ok(minNode);
    }

}
